package com.cimp.service.c2;

import com.cimp.dao.c2.UpstreamDao;
import com.cimp.domain.c2.Distribute;
import com.cimp.domain.c2.DistributeMsg;
import com.cimp.domain.c2.SoapConfig;
import com.cimp.domain.iqy.Virtual;
import com.cimp.service.c2.client.CSPRequest;
import com.cimp.service.c2.client.CSPRequestServiceLocator;
import com.cimp.service.c2.client.CSPResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import sun.security.util.DisabledAlgorithmConstraints;

import javax.annotation.Resource;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2019/1/7
 */
@Service
public class UpstreamService {

    @Resource
    private UpstreamDao upstreamDao;


    public void insertSoapConfig(SoapConfig soapConfig) {
        upstreamDao.insertSoapConfig(soapConfig);
    }

    public void updateSoapConfig(SoapConfig soapConfig) {
        upstreamDao.updateSoapConfig(soapConfig);
    }

    public List<SoapConfig> querySoapConfigList() {
        return upstreamDao.listSoapConfig();
    }

    public void deleteSoapConfig(Integer id) {
        upstreamDao.deleteSoapConfig(id);
        upstreamDao.deleteDistributeByConfigId(id);
    }

    public void distribute(SoapConfig soapConfig) throws ServiceException, RemoteException, MalformedURLException {
        Distribute distribute = null;
        try {
            CSPRequestServiceLocator cspRequestServiceLocator = new CSPRequestServiceLocator();
            cspRequestServiceLocator.setctmsEndpointAddress(soapConfig.getUrl());
            CSPRequest request = cspRequestServiceLocator.getctms();
            SimpleDateFormat format = new SimpleDateFormat("HHmmssSSS");
            String correlateId = format.format(new Date());
            String cspid = soapConfig.getCspid();
            String lspid = soapConfig.getLspid();
            String cmdFileUrl = soapConfig.getCmdFileUrl();
            distribute = this.insertDistribute(soapConfig.getId(), correlateId, "处理中");
            CSPResult cspResult = request.execCmd(cspid, lspid, correlateId, cmdFileUrl);
            // 如果下发失败
            if (cspResult == null ) {
                throw new RuntimeException("下发失败");
            }
            if (cspResult.getResult() != 0) {
                String result = StringUtils.isBlank(cspResult.getErrorDescription()) ? "发送失败" : cspResult.getErrorDescription();
                throw new RuntimeException(result);
            }

        } catch (Exception e) {
            if (distribute != null) {
                upstreamDao.updateDistributeState(distribute.getId(),e.getMessage());
            }
            throw e;
        }


    }

    public void updateDistributeState(String correlateId, String state){
        upstreamDao.updateDistributeStateByCorrelateId(correlateId,state);
    }

    public Distribute insertDistribute(Integer configId,String correlateId, String state){
        Distribute distribute = new Distribute(null, correlateId, configId, state);
        upstreamDao.insertDistribute(distribute);
        return distribute;
    }

    public List<DistributeMsg> queryDistributeMsgList() {
        return upstreamDao.listDistributeMsg();
    }
}
